# -*- coding: utf-8 -*-

'''
根据json配置中Item_Define中的item定义,动态定义EasySpiderItem
'''

import scrapy
from scrapy.utils.project import get_project_settings


SETTINGS = get_project_settings()
ITEMS_DEFINE = SETTINGS.getdict('ITEMS_DEFINE')


class EasySpiderItem(scrapy.Item):

    @classmethod
    def add_item(cls, name, field):
        assert isinstance(field, scrapy.Field), "argument <field> must be instance of scrapy.Field"
        cls.fields[name] = field

    @classmethod
    def add_items(cls, fields):
        assert isinstance(fields, dict), "argument <fields> must be a dict"
        for k, v in fields.items():
            cls.add_item(k, scrapy.Field(xpath=v['xpath'], re=v['re'], strict=v['strict']))


EasySpiderItem.add_items(ITEMS_DEFINE)